<?php

import('classes.handler.Handler');
class SSTKHandler extends Handler {
    function SS_TK($args, &$request) {
		// chua dung den

        $gcNam = Request::getUserVar('xNam') ;
        $xSection = Request::getUserVar("ChiTiet") ;
        $xDay  = Request::getUserVar("dateExpire1Day");
        $xMonth  = Request::getUserVar("dateExpire1Month");

        $xDay2  = Request::getUserVar("dateExpire2Day");
        $xMonth2  = Request::getUserVar("dateExpire2Month");

        $xmatk = Request::getUserVar("matk");

        $user =& Request::getUser();
        $userId = $user->getId();
        $xma_dvcs = $user->getPath();
        $date1 = mktime(0, 0, 0, $xMonth, $xDay, $gcNam);
        $date2 = mktime(0, 0, 0, $xMonth2, $xDay2, $gcNam);


       // lay so lieu
        $ctktDao =& DAORegistry::getDAO('CTKTDAO');
        $ctkts =& $ctktDao->getCTKT_BC1($gcNam, $date1, $date2, $xmatk);
        //var_dump($ctkts);

        $templateMgr =& TemplateManager::getManager();
        $templateMgr->assign('gcNam', $gcNam);
        $templateMgr->assign('TBC', "So Quy" );
        $templateMgr->assign('ctkts', $ctkts);
        $templateMgr->assign('tencty','Công ty dược TW3');
        $templateMgr->assign('diachi','Địa chỉ: Đà Nẵng');
        $templateMgr->assign('tieude','SỔ QUỸ TIỀN MẶT');
        $templateMgr->assign('loaitk',$xmatk);
        $templateMgr->assign('tbdau',$date1);
        $templateMgr->assign('tkthuc',$date2);
        $templateMgr->assign('section',$xSection);


        $templateMgr->display('acctSS/soquy.tpl');
    }


    function SS_KTTH_STHTCG($args, &$request) {
		$loai  = Request::getUserVar("loai");
		$journal =& Request::getJournal();
		$journalid =$journal->getId();
		$ngay1 = Request::getUserVar("ngay1");
		$ngay2 = Request::getUserVar("ngay2");
		$gcNam = Request::getUserVar("nam");
		$matentk = explode("-", Request::getUserVar("matk"));
		$matk  = $matentk[0];
		$tentk = $matentk[1];
        $user =& Request::getUser();
        $userId = $user->getId();

        // lay so lieu
        $ctktthDao =& DAORegistry::getDAO('CTKTTHDAO');
		$dict = NewDataDictionary($ctktthDao->_dataSource);
		// Make sure the table exists
		$tables = $dict->MetaTables('TABLES', false);
		$tablename = "ctktth".$journalid. $gcNam;
		$ectktth = 1;
		if (!in_array($tablename,$tables)) 
		{
			$ectktth = 0;
		
		}
		$tablename = "ctkt".$journalid. $gcNam;
		$ectkt = 1;
		if (!in_array($tablename,$tables)) 
		{
			$ectkt = 0;
		
		}
		if ($ectktth == 0 && $ectkt ==0)
			echo '<script type="text/javascript">alert("Khong co du lieu!"); window.close(); </script>'; 

		
	    $ctkts =& $ctktthDao->getCTKTTH_STHTCG($loai, $journalid, $gcNam, $ngay1, $ngay2, $matk, $ectkt, $ectktth);
		$tcs = $ctkts['tc'];
		unset($ctkts['tc']);
		$tks = $ctkts['tk'];
		sort($tks, SORT_STRING );
		$tkcount =  sizeof($tks);
		unset($ctkts['tk']);

		
		
		$templateMgr =& TemplateManager::getManager();
		$templateMgr->assign('tentk',$tentk);
        $templateMgr->assign('tbdau',$ngay1);
        $templateMgr->assign('tkthuc',$ngay2);
		
        $templateMgr->assign('TBC', "So TMTG" );
        $templateMgr->assign_by_ref('ctkts', $ctkts);
		$templateMgr->assign_by_ref('tks', $tks);
		$templateMgr->assign_by_ref('tcs', $tcs);

		$templateMgr->assign('dkno', $spsno);
		$templateMgr->assign('dkco', $spsco);
        $templateMgr->assign('tencty','Công ty dược TW3');
        $templateMgr->assign('diachi','Địa chỉ: Đà Nẵng');
		

        $templateMgr->assign('loaitk',$matk);
		$templateMgr->assign('tkcount',$tkcount);
		$templateMgr->assign('noco',$loai);


		
		$templateMgr->assign('tieude','BẢNG TỔNG HỢP CHỨNG TỪ GỐC CÙNG LOẠI');

		$templateMgr->display('acctSS/thctg.tpl');

	}

function SS_KTTH_BCTC_TKBC($args, &$request) {
	$templateMgr =& TemplateManager::getManager();
	$templateMgr->addJavaScript('lib/pkp/js/lib/jquery/plugins/validate/jquery.validate.min.js');
		$templateMgr->addJavaScript('lib/pkp/js/jqueryValidatorI18n.js');
	$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
			// them phan Search


			$user =& Request::getUser();
			$userId = $user->getId();
			$xma_dvcs = $user->getPath();


			$searchType = null;
			$searchMatch = null;
			$search = Request::getUserVar('search');

			$searchInitial = Request::getUserVar('searchInitial');
			if (!empty($search)) {
				$searchType = Request::getUserVar('searchField');
				$searchMatch = Request::getUserVar('searchMatch');

			} elseif (!empty($searchInitial)) {
				$searchInitial = String::strtoupper($searchInitial);
				$searchType = USER_FIELD_INITIAL;
				$search = $searchInitial;
			}

			$fieldOptions = Array(
			DMTK_FIELD_MATK => 'M&#227; TK',
			DMTK_FIELD_TENTK => 'T&#234;n TK'
				);

			$templateMgr->assign('fieldOptions', $fieldOptions);

			$templateMgr->assign('searchField', $searchType);
			$templateMgr->assign('searchMatch', $searchMatch);
			$templateMgr->assign('search', $search);
			$templateMgr->assign('searchInitial', Request::getUserVar('searchInitial'));

			if (!empty($search)) {
			$fieldName = $searchType;
			switch ($searchMatch) {
				case 'is':
					$searchSql = "($fieldName = $search)";
					break;
				case 'contains':
					$searchSql = "($fieldName có chứa $search)";
					break;
				case 'startsWith':
					$searchSql = "($fieldName bắt đầu là $search)";
					break;
				}
			}
			$templateMgr->assign('searchSql', $searchSql);

			$dmtks =& $dmtkDao->getDMTKSearch($searchType, $search, $searchMatch);
			$templateMgr->assign_by_ref('dmtks', $dmtks);
			$templateMgr->assign('SS_SecDetail', "a");
			$templateMgr->addJavaScript('classes/acct/js/danhmuc.js');
			$templateMgr->addJavaScript("lib/pkp/js/lib/jquery/jquery.min.js");
			$templateMgr->addStyleSheet($request->getBaseUrl().'/styles/dm.css');

			$templateMgr->display('acctSS/cdtk_tkbc.tpl');
}
function SS_KTTH_BCTC_TKBC_luu($args, &$request) {
	$bcc = Request::getUserVar('bcc');
	$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
	$dstk= "";
	foreach ($bcc as $key => $value) {
		if ($dstk == "")
			$dstk= "'".$value."'";
		else
			$dstk= $dstk. ", '".$value."'";

	}
	$dmtkDao->updatecdtk($dstk);
	$newurl = Request::url(null, null, "SS_KTTH_BCTC_TKBC"); 
	PKPRequest::redirectUrl($newurl);
}

function SS_KTTH_BCTC($args, &$request) {
		
	$chitiet = Request::getUserVar("ChiTiet"); 
	if (substr($chitiet,0,1) == 1)
	{
		$this->SS_KTTH_CDTK();
	}
	else
	{
		$this->SS_KTTH_CDKT();
	}
}

function SS_KTTH_CDKT() {
}

function SS_KTTH_CDTK() {
	$th1= Request::getUserVar("dateExpire1Month"); 
	$th0 = $th1 -1;
	$th2= Request::getUserVar("dateExpire2Month"); 
	$nam= Request::getUserVar("dateExpire2Year"); 
	$ng0 = "$nam-01-01";
	$ng02 = "$nam-$th0-01";
	$ng1 = "$nam-$th1-01";
	$ng2 = "$nam-$th2-01";
	$ngay1 =  date("Y-m-d", strtotime($ng1));
	$ngay2 =  date("Y-m-t", strtotime($ng2));
	$ngay0 =  date("Y-m-d", strtotime($ng0));
	$ngay02 = date("Y-m-t", strtotime($ng02));

	$journal =& Request::getJournal();
	$journalid =$journal->getId();



	$ctktthDao =& DAORegistry::getDAO('CTKTTHDAO');
	$dict = NewDataDictionary($ctktthDao->_dataSource);
	$tables = $dict->MetaTables('TABLES', false);
	$tablename = "sdtk".$journalid. $nam;
	if (!in_array($tablename,$tables)) 
	{
		echo '<script type="text/javascript">alert("Chua cap nhat so du dau nam!"); window.close(); </script>';
	}

	// Make sure the table exists
	
	$tablename = "ctktth".$journalid. $nam;
	$ectktth = 1;
	if (!in_array($tablename,$tables)) 
	{
		$ectktth = 0;
	
	}
	$tablename = "ctkt".$journalid. $nam;
	$ectkt = 1;
	if (!in_array($tablename,$tables)) 
	{
		$ectkt = 0;
	
	}
	if ($ectktth == 0 && $ectkt ==0)
		echo '<script type="text/javascript">alert("Khong co du lieu!"); window.close(); </script>'; 

	// lay dmtk:
	$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
	$data = $dmtkDao->getDS_CDTK();
	$datatc= array(); 
	$datatc['tk'] = 'Tổng cộng';
	//var_dump($result);

	// lay so du dau ky:
	$dataDK =& $ctktthDao->getBCTC_SDDK($journalid, $nam);
	$dataPS =& $ctktthDao->getBCTC_PS($journalid, $nam, $ngay1, $ngay2, $ectkt, $ectktth);
	$dataPS0 =& $ctktthDao->getBCTC_PS($journalid, $nam, $ngay0, $ngay02, $ectkt, $ectktth);
	// merge aray
	//$data=$dataDK ;
	foreach ($dataDK as $key => $value) {
		if (isset($data[$key]["tk"]))
		{
			$data[$key]["dknovn"] = $value["dknovn"];
			$data[$key]["dkcovn"] = $value["dkcovn"];
			$data[$key]["cknovn"] = $value["dknovn"];
			$data[$key]["ckcovn"] = $value["dkcovn"];

		}
		//cong vao tk cha
		$datatc["dkcovn"] = $datatc["dkcovn"] + $value["dkcovn"];
		$datatc["dknovn"] = $datatc["dknovn"] + $value["dknovn"];
		$datatc["ckcovn"] = $datatc["ckcovn"] + $value["dkcovn"];
		$datatc["cknovn"] = $datatc["cknovn"] + $value["dknovn"];


		for ($i = 2 ; $i < strlen($key); $i++)
		{
			$newkey = substr($key,0, $i);
			if (isset($data[$newkey]["tk"]))
			{
				$data[$newkey]["tkcha"] = 1;
				$data[$newkey]["dknovn"] = $data[$newkey]["dknovn"] + $value["dknovn"];
				$data[$newkey]["dkcovn"] = $data[$newkey]["dkcovn"]+ $value["dkcovn"];
				$data[$newkey]["cknovn"] = $data[$newkey]["cknovn"] + $value["dknovn"];
				$data[$newkey]["ckcovn"] = $data[$newkey]["ckcovn"]+ $value["dkcovn"];
			}
		}
	}

	foreach ($dataPS as $key => $value) {

		if (isset($data[$key]["tk"]))
		{
			$data[$key]["pscovn"] = $value["pscovn"];
			$data[$key]["psnovn"] = $value["psnovn"];
			$data[$key]["pscovnlk"] = $value["pscovn"];
			$data[$key]["psnovnlk"] = $value["psnovn"];

			$data[$key]["cknovn"] = $data[$key]["cknovn"]  + $value["psnovn"]  ;
			$data[$key]["ckcovn"] = $data[$key]["ckcovn"]  + $value["pscovn"];
		}
		//cong vao tk cha
		$datatc["pscovn"] = $datatc["pscovn"] + $value["pscovn"];
		$datatc["psnovn"] = $datatc["psnovn"] + $value["psnovn"];
		$datatc["pscovnlk"] = $datatc["pscovn"];
		$datatc["psnovnlk"] = $datatc["psnovn"];
		$datatc["cknovn"] = $datatc["cknovn"]  + $value["psnovn"]  ;
		$datatc["ckcovn"] = $datatc["ckcovn"]  + $value["pscovn"];

		for ($i = 2 ; $i < strlen($key); $i++)
		{
			$newkey = substr($key,0, $i);

			if (isset($data[$newkey]["tk"]))
			{
				$data[$newkey]["tkcha"] = 1;
				$data[$newkey]["psnovn"] = $data[$newkey]["psnovn"] + $value["psnovn"];
				$data[$newkey]["pscovn"] = $data[$newkey]["pscovn"] + $value["pscovn"];
				$data[$newkey]["pscovnlk"] = $data[$newkey]["pscovn"] ;
				$data[$newkey]["psnovnlk"] = $data[$newkey]["psnovn"] ;
				$data[$newkey]["cknovn"] = $data[$newkey]["cknovn"]  + $value["psnovn"]  ;
				$data[$newkey]["ckcovn"] = $data[$newkey]["ckcovn"]  + $value["pscovn"];
				
			}
		}

	}

	// lay them so ky truoc
	foreach ($dataPS0 as $key => $value) {
		if (isset($data[$key]["tk"]))
		{
			$data[$key]["dknovn"] = $data[$key]["dknovn"] + $value["psnovn"];
			$data[$key]["dkcovn"] = $data[$key]["dkcovn"] + $value["pscovn"];
			$data[$key]["psnovnlk"] = $value["psnovn"] + $data[$key]["psnovnlk"];
			$data[$key]["pscovnlk"] = $value["pscovn"] + $data[$key]["pscovnlk"];
			$data[$key]["cknovn"] = $data[$key]["cknovn"]  + $value["psnovn"]  ;
			$data[$key]["ckcovn"] = $data[$key]["ckcovn"]  + $value["pscovn"];


		}

		//cong vao tk cha
		$datatc["dknovn"] = $datatc["dknovn"] + $value["psnovn"];
		$datatc["dkcovn"] = $datatc["dkcovn"]+ $value["pscovn"];;

		$datatc["pscovnlk"] = $datatc["pscovnlk"] + $value["pscovn"];
		$datatc["psnovnlk"] = $datatc["psnovnlk"]+ $value["psnovn"];

		$datatc["cknovn"] = $datatc["cknovn"] + $value["psnovn"];
		$datatc["ckcovn"] = $datatc["ckcovn"]+ $value["pscovn"];;

		for ($i = 2 ; $i < strlen($key); $i++)
		{
			$newkey = substr($key,0, $i);

			if (isset($data[$newkey]["tk"]))
			{
				$data[$newkey]["tkcha"] = 1;
				$data[$newkey]["dknovn"] = $data[$newkey]["dknovn"] + $value["psnovn"];
				$data[$newkey]["dkcovn"] = $data[$newkey]["dkcovn"] + $value["pscovn"];
				$data[$newkey]["psnovnlk"] = $value["psnovn"] + $data[$newkey]["psnovnlk"] ;
				$data[$newkey]["pscovnlk"] = $value["pscovn"] +  $data[$newkey]["pscovnlk"] ;
				$data[$newkey]["cknovn"] = $data[$newkey]["cknovn"] + $value["psnovn"];
				$data[$newkey]["ckcovn"] = $data[$newkey]["ckcovn"]+ $value["pscovn"];;

			}
		}

	}

	// tat toan cuoi ky
	$datatc["dknovn"] = 0;
	$datatc["dkcovn"] = 0;
	$datatc["cknovn"] = 0;
	$datatc["ckcovn"] = 0;

	$chitiet = Request::getUserVar("ChiTiet"); 
	if (strpos($chitiet,"b") === false)	{
	foreach ($data as $key => $value) {

		$data[$key]["dknovn"] = $data[$key]["dknovn"]  - $data[$key]["dkcovn"];
		if ($data[$key]["dknovn"]  >0)
			$data[$key]["dkcovn"]  =0;
		else
		{
			$data[$key]["dkcovn"]  =- $data[$key]["dknovn"] ;
			$data[$key]["dknovn"]  =0;
		}

		$data[$key]["cknovn"] = $data[$key]["dknovn"]  - $data[$key]["dkcovn"]  + $data[$key]["psnovn"] -		$data[$key]["pscovn"] ;
		if ($data[$key]["cknovn"]  >0)
			$data[$key]["ckcovn"]  =0;
		else
		{
			$data[$key]["ckcovn"]  =- $data[$key]["cknovn"] ;
			$data[$key]["cknovn"]  =0;
		}
		if ($data[$key]["tkcha"] != 1)
		{
			$datatc["dknovn"] = $datatc["dknovn"] + $data[$key]["dkcovn"];
			$datatc["dkcovn"] = $datatc["dkcovn"]+ $data[$key]["dkcovn"];
			$datatc["cknovn"] = $datatc["cknovn"] + $data[$key]["ckcovn"];
			$datatc["ckcovn"] = $datatc["ckcovn"]+ $data[$key]["ckcovn"];
		}
	}
	}
	else{
	foreach ($data as $key => $value) {
		if ($data[$key]["dknovn"] + $data[$key]["dkcovn"] + $data[$key]["psnovnlk"] + $data[$key]["pscovnlk"] ==0)
		{
			unset($data[$key]);
			continue;
		}

		$data[$key]["dknovn"] = $data[$key]["dknovn"]  - $data[$key]["dkcovn"];
		if ($data[$key]["dknovn"]  >0)
			$data[$key]["dkcovn"]  =0;
		else
		{
			$data[$key]["dkcovn"]  =- $data[$key]["dknovn"] ;
			$data[$key]["dknovn"]  =0;
		}

		$data[$key]["cknovn"] = $data[$key]["dknovn"]  - $data[$key]["dkcovn"]  + $data[$key]["psnovn"] -		$data[$key]["pscovn"] ;
		if ($data[$key]["cknovn"]  >0)
			$data[$key]["ckcovn"]  =0;
		else
		{
			$data[$key]["ckcovn"]  =- $data[$key]["cknovn"] ;
			$data[$key]["cknovn"]  =0;
		}
		if ($data[$key]["tkcha"] != 1)
		{
			$datatc["dknovn"] = $datatc["dknovn"] + $data[$key]["dkcovn"];
			$datatc["dkcovn"] = $datatc["dkcovn"]+ $data[$key]["dkcovn"];
			$datatc["cknovn"] = $datatc["cknovn"] + $data[$key]["ckcovn"];
			$datatc["ckcovn"] = $datatc["ckcovn"]+ $data[$key]["ckcovn"];
		}
	}
	}

//	$datatc["dknovn"] = $datatc["dknovn"]  - $datatc["dkcovn"];
//		if ($datatc["dknovn"]  >0)
//			$datatc["dkcovn"]  =0;
//		else
//		{
//			$datatc["dkcovn"]  =- $datatc["dknovn"] ;
//			$datatc["dknovn"]  =0;
//		}

//		$datatc["cknovn"] = $datatc["dknovn"]  - $datatc["dkcovn"]  + $value["psnovn"] -		$value["pscovn"] ;
//		if ($datatc["cknovn"]  >0)
//			$datatc["ckcovn"]  =0;
//		else
//		{
//			$datatc["ckcovn"]  =- $datatc["cknovn"] ;
//			$datatc["cknovn"]  =0;
//		}


		$templateMgr =& TemplateManager::getManager();
        $templateMgr->assign('tbdau',$ngay1);
        $templateMgr->assign('tkthuc',$ngay2);
		

        $templateMgr->assign_by_ref('ctkts', $data);
		$templateMgr->assign_by_ref('tc', $datatc);


        $templateMgr->assign('tencty','Công ty dược TW3');
        $templateMgr->assign('diachi','Địa chỉ: Đà Nẵng');
		

		$templateMgr->assign('tieude','BẢNG CÂN ĐỐI TÀI KHOẢN');

		$templateMgr->display('acctSS/cdtk.tpl');
		return;
	
	}

    function SS_KTTH_Socai($args, &$request) {
		$journal =& Request::getJournal();
		$journalid =$journal->getId();
		$ngay1 = Request::getUserVar("ngay1");
		$ngay2 = Request::getUserVar("ngay2");
		$gcNam = Request::getUserVar("nam");
		$matentk = explode("-", Request::getUserVar("matk"));
		$matk  = $matentk[0];
		$tentk = $matentk[1];
        $user =& Request::getUser();
        $userId = $user->getId();

        // lay so lieu
        $ctktthDao =& DAORegistry::getDAO('CTKTTHDAO');
		$dict = NewDataDictionary($ctktthDao->_dataSource);
		// Make sure the table exists
		$tables = $dict->MetaTables('TABLES', false);
		$tablename = "ctktth".$journalid. $gcNam;
		$ectktth = 1;
		if (!in_array($tablename,$tables)) 
		{
			$ectktth = 0;
		
		}
		$tablename = "ctkt".$journalid. $gcNam;
		$ectkt = 1;
		if (!in_array($tablename,$tables)) 
		{
			$ectkt = 0;
		
		}
		if ($ectktth == 0 && $ectkt ==0)
			echo '<script type="text/javascript">alert("Khong co du lieu!"); window.close(); </script>'; 

		$tablename = "sdtk".$journalid. $gcNam;
		if (!in_array($tablename,$tables)) 
		{
			echo '<script type="text/javascript">alert("Chua cap nhat so du dau nam!"); window.close(); </script>';
		}
	    $ctkts =& $ctktthDao->getCTKTTH_socai($journalid, $gcNam, $ngay1, $ngay2, $matk, $ectkt, $ectktth);

		if ($ectkt)
			$psdks_tmtg =& $ctktthDao->getCTKTTH_socai_PSDK_TMTG($journalid, $gcNam, $ngay1, $ngay2, $matk);
		else
			$psdks_tmtg = array("dknovn"=>0, "dkcovn"=>0);

		if ($ectktth)
			$psdks_ktth =& $ctktthDao->getCTKTTH_socai_PSDK_KTTH($journalid, $gcNam, $ngay1, $ngay2, $matk);
		else
			$psdks_ktth = array("dknovn"=>0, "dkcovn"=>0);


		$sodudks =& $ctktthDao->getCTKTTH_socai_SDDK($journalid, $gcNam, $ngay1, $ngay2, $matk, $mant);

		$spsno = $sodudks["dknovn"] + $psdks_tmtg["psnovn"] +$psdks_ktth["psnovn"]- 
			$sodudks["dkcovn"] - $psdks_tmtg["pscovn"] -$psdks_ktth["pscovn"]
			;
		if ($spsno >=0)
			$spsco = 0;
		else
		{
			$spsco = -$spsno;
			$spsno = 0;
		}
		$templateMgr =& TemplateManager::getManager();
		$templateMgr->assign('tentk',$tentk);
        $templateMgr->assign('tbdau',$ngay1);
        $templateMgr->assign('tkthuc',$ngay2);
		$templateMgr->addJavaScript("classes/acct/js/soquy.js");

        $templateMgr->assign('TBC', "So TMTG" );
        $templateMgr->assign_by_ref('ctkts', $ctkts);

		$templateMgr->assign('dkno', $spsno);
		$templateMgr->assign('dkco', $spsco);
        $templateMgr->assign('tencty','Công ty dược TW3');
        $templateMgr->assign('diachi','Địa chỉ: Đà Nẵng');
		$templateMgr->assign('mant',$mant);

        $templateMgr->assign('loaitk',$matk);
		
		$templateMgr->assign('tieude','SỔ CÁI TÀI KHOẢN');
		$templateMgr->display('acctSS/socai.tpl');
		
    }   




}

?>
